<template>
  <el-dialog
    title="新增员工"
    :visible="showDialog"
    width="50%"
    @close="btnConcel"
  >
    <el-form
      ref="employeeFormRef"
      :model="formData"
      :rules="formRules"
      label-width="120px"
    >
      <el-form-item label="姓名" prop="username">
        <el-input
          v-model="formData.username"
          style="width: 80%"
          placeholder="请输入姓名"
        />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input
          v-model="formData.mobile"
          style="width: 80%"
          placeholder="请输入手机号"
        />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker
          v-model="formData.timeOfEntry"
          style="width: 80%"
          type="date"
          placeholder="选择日期"
        />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select
          v-model="formData.formOfEmployment"
          placeholder="请选择"
          style="width: 80%"
        >
          <el-option
            v-for="item in EmployeesEnum.hireType"
            :key="item.id"
            :value="item.id"
            :label="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input
          v-model="formData.workNumber"
          style="width: 80%"
          placeholder="请输入工号"
        />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input
          v-model="formData.departmentName"
          style="width: 80%"
          placeholder="请输入部门"
          @focus="getDepartments"
        />
        <el-tree
          v-if="showTree"
          v-loading="loading"
          :data="departs"
          :props="defaultProps"
          :default-expand-all="true"
          @node-click="onTreeNodeClick"
        />
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker
          v-model="formData.correctionTime"
          style="width: 80%"
          type="date"
          placeholder="选择日期"
        />
      </el-form-item>
    </el-form>
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button type="primary" size="small" @click="onOK">确 定</el-button>
        <el-button size="small" @click="btnConcel">取 消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { getDepartments } from '@/api/departments'
import { tranListToTreeData } from '@/utils'
import EmployeesEnum from '@/api/constant/employees.js'
import { addEmployee } from '@/api/employees'
export default {
  name: 'AddEmployee',
  components: {

  },
  props: {
    showDialog: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      EmployeesEnum,
      // 表单数据
      formData: {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      },
      // 校验规则
      formRules: {
        username: [
          { required: true, message: '姓名不能为空', trigger: 'blur' },
          { min: 1, max: 4, message: '姓名长度为1 - 4位', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式错误', trigger: 'blur' }
        ],
        formOfEmployment: [{ required: true, message: '请选择聘用形式', trigger: 'blur' }],
        workNumber: [{ required: true, message: '工号不能为空', trigger: 'blur' }],
        departmentName: [{ required: true, message: '请选择部门', trigger: 'change' }],
        timeOfEntry: [{ required: true, message: '入职时间不能为空', trigger: 'blur' }]
      },
      departs: [],
      showTree: false,
      loading: false,
      defaultProps: {
        label: 'name'
      }
    }
  },
  created() {

  },
  methods: {
    async getDepartments() {
      this.showTree = true
      this.loading = true
      const { depts } = await getDepartments()
      // 方式2 传入 id 以便子组件查询兄弟部门
      this.departs = tranListToTreeData(depts)
      this.loading = false
    },
    onTreeNodeClick(data) {
      this.formData.departmentName = data.name
      this.showTree = false
    },
    onOK() {
      this.$refs.employeeFormRef.validate(async isOk => {
        if (isOk) {
          await addEmployee(this.formData)
          this.$parent.getEmployeeList && this.$parent.getEmployeeList()
          this.$parent.showDialog = false
          this.$message.success('添加员工成功')
        }
      })
    },
    btnConcel() {
      this.formData = this.$options.data().formData
      this.$emit('update:showDialog', false)
      this.$refs.employeeFormRef.resetFields()
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
